package top.huxy.boot.util.obj;

import com.alibaba.fastjson.JSON;
import top.huxy.boot.util.DateUtils;

import java.util.Date;
import java.util.HashMap;

public class Result {
    public int code;
    public Object data;
    public String msg;
    public String responseTime = DateUtils.formatAsDateTime(new Date());
    public long processingTime;

    public HashMap<String, Object> remarks;

    public Result(int code, String msg, Object data) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static Result clientError(Object data) {
        return new Result(400, "error", data);
    }

    public static Result clientError(Object data, String msg) {
        return new Result(400, msg, data);
    }

    public static Result success(Object data) {
        return new Result(200, "ok", data);
    }

    public static Result success(Object data, String msg) {
        return new Result(200, msg, data);
    }

    public static Result notFound(Object data) {
        return new Result(404, "not found", data);
    }

    public static Result notFound(Object data, String msg) {
        return new Result(404, msg, data);
    }

    public static Result serverError(String data) {
        return new Result(500, "server error", data);
    }

    public static Result serverError(String data, String msg) {
        return new Result(500, msg, data);
    }

    public void setProcessingTime(long time) {
        this.processingTime = time;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public void addRemake(String key, Object o) {
        if (remarks == null) {
            remarks = new HashMap<>(5);
        }
        remarks.put(key, o);
    }

    public HashMap<String, Object> getRemarks() {
        return remarks;
    }
}
